uniform sampler2D tex;
uniform int size;

varying float s;
varying float t;

void main(void)
{
	float grid = 1.0 / float(size);
	
	vec4 left = texture2D(tex, vec2(s - grid, t));
	vec4 right = texture2D(tex, vec2(s + grid, t));
	vec4 down = texture2D(tex, vec2(s, t - grid));
	vec4 up = texture2D(tex, vec2(s, t + grid));
	
	float x1 = right.x - left.x;
	float y1 = right.y - left.y;
	float z1 = right.z - left.z;	
	float x2 = up.x - down.x;
	float y2 = up.y - down.y;
	float z2 = up.z - down.z;
	
	vec3 normal = normalize(vec3(y1 * z2 - y2 * z1, z1 * x2 - z2 * x1, x1 * y2 - x2 * y1));
	
	gl_FragColor = vec4(normal, 1.0);
}